package edu.uwlax.cs.oayonlinestore.client.gui;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.User;
import edu.uwlax.cs.oayonlinestore.client.User.UserChangeListener;
import edu.uwlax.cs.oayonlinestore.client.utils.StackPage;
import edu.uwlax.cs.oayonlinestore.client.utils.SwitchPage;

public class Store {

	private class MainMenu extends StyleMenu implements UserChangeListener {

		public MainMenu() {
			super(false, STYLE_MENU, STYLE_MENU_ITEM);
			setAutoOpen(true);
			userChanged(User.get());
			User.get().addChangeListener(this);
		}

		public void userChanged(User user) {
			clearItems();
			addItem("Home", new SwitchPage(frame.getHome())).addStyleName(
					STYLE_MENU_ITEM);

			// shop
			addItem("Shop", new SwitchPage(frame.getShop()));
			
			//track
			addItem("Track Orders", new SwitchPage(frame.getTrack()));

			// my Account
			if (user.getId() != 0) {
				StyleMenu acc = new StyleMenu(true, STYLE_SUBM, STYLE_SUBM_ITEM);
				acc.addItem("my Profile", frame.getUsrEditAcc());
				acc.addItem("my Orders", frame.getUsrListOrders());
				addItem("my Account", acc);
			}

			// stock
			if (user.isCanBeCategoryPerson()) {
				StyleMenu stk = new StyleMenu(true, STYLE_SUBM, STYLE_SUBM_ITEM);
				stk.addItem("Items", frame.getStockCategories());
				stk.addItem("Inventory Report", frame.getSalInventoryRep());
				addItem("Stock", stk);
			}

			// sales
			if (user.isIsSalesManager() || user.isIsSalesStaff()) {
				StyleMenu sls = new StyleMenu(true, STYLE_SUBM, STYLE_SUBM_ITEM);
				sls.addItem("Orders", frame.getSalOrders());
				sls.addItem("Return/Exchange", frame.getSalListRetex());
				sls.addItem("Gift Cards", frame.getSalGiftCards());
				if (user.isIsSalesManager()) {
					sls.addItem("Prices", frame.getSalPrices());
					sls.addItem("Sales Report", frame.getSalSalesRep());
				}
				addItem("Sales", sls);
			}

			// admin
			if (user.isIsAdministrator()) {
				StyleMenu adm = new StyleMenu(true, STYLE_SUBM, STYLE_SUBM_ITEM);
				adm.addItem("Accounts", frame.getAdminListAcc());
				adm.addItem("Industrial Users", frame.getAdminIndust());
				adm.addItem("Categories", frame.getAdminCategories());
				addItem("Admin", adm);
				addItem("&nbsp;", true, new SwitchPage(frame.getTest()));
			}
		}

	}

	private class StyleMenu extends MenuBar {

		private String itemStyle;

		public StyleMenu(boolean vertical, String menuStyle, String itemStyle) {
			super(vertical);
			setStyleName(menuStyle);
			this.itemStyle = itemStyle;
		}

		public MenuItem addItem(String arg0, boolean arg1, Command arg2) {
			MenuItem i = super.addItem(arg0, arg1, arg2);
			i.setStyleName(itemStyle);
			return i;
		}

		public MenuItem addItem(String arg0, boolean arg1, MenuBar arg2) {
			MenuItem i = super.addItem(arg0, arg1, arg2);
			i.setStyleName(itemStyle);
			return i;
		}

		public MenuItem addItem(String arg0, Command arg1) {
			MenuItem i = super.addItem(arg0, arg1);
			i.setStyleName(itemStyle);
			return i;
		}

		public MenuItem addItem(String arg0, MenuBar arg1) {
			MenuItem i = super.addItem(arg0, arg1);
			i.setStyleName(itemStyle);
			return i;
		}

		public MenuItem addItem(String arg0, StackPage arg1) {
			MenuItem i = super.addItem(arg0, new SwitchPage(arg1));
			i.setStyleName(itemStyle);
			return i;
		}

	}

	private static Store instance;

	private static final String STYLE_MENU = "Menu";

	private static final String STYLE_MENU_ITEM = "MenuItem";

	private static final String STYLE_SUBM = "MenuSub";

	private static final String STYLE_SUBM_ITEM = "MenuSubItem";

	public static Store get() {
		if (instance == null)
			instance = new Store();
		return instance;
	}

	public static MainPanel getFrame() {
		return get().frame;
	}

	private MainPanel frame = new MainPanel(null);

	public Widget init() {
		Design d = new Design();
		d.setMenu(new MainMenu());
		d.setLogin(new Login());
		d.setContent(frame);
		frame.init();
		return d;
	}
}
